/**
 * 
 */
package com.css.crm.service.impl;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;

import com.css.crm.exception.SystemException;
import com.css.crm.message.SystemErrorMessage;
import com.css.crm.service.SolrService;
import com.css.crm.utility.ConstantUtil;
import com.css.crm.utility.ResourceUtil;
import com.css.crm.vo.CustomerEmailVo;
import com.css.crm.vo.CustomerTelephoneVo;
import com.css.crm.vo.CustomerVo;

/**
 * @author AIS
 * 
 */
public class SolrServiceImpl implements SolrService{

	@Override
	public void createDocument(CustomerVo customerVo) throws SystemException {
		SolrInputDocument doc = new SolrInputDocument();
		doc.addField("custId", customerVo.getCustId());
		doc.addField("namEng", customerVo.getNamEng());
		doc.addField("namZht", customerVo.getNamZht());
		doc.addField("namZhs", customerVo.getNamZhs());
		doc.addField("birthday", customerVo.getDobMonth() + "-" + customerVo.getDobDay());
		
		if(customerVo.getCustomerEmailList() != null){
			for(CustomerEmailVo emailVo : customerVo.getCustomerEmailList()){
				if(ConstantUtil.COMMUNICATE_TYPE_CONTACT.equals(emailVo.getTypeCde())){
					doc.addField("email", emailVo.getEmail());
				}
			}
		}
		
		if(customerVo.getCustomerTelephoneList() != null){
			for(CustomerTelephoneVo telephoneVo : customerVo.getCustomerTelephoneList()){
				if(ConstantUtil.COMMUNICATE_TYPE_CONTACT.equals(telephoneVo.getTypeCde())){
					doc.addField("telNbr", telephoneVo.getTelNbr());
				}
			}
		}		

		String solrUrl = ResourceUtil.getSolrConfig().get(ConstantUtil.SOLR_SERVER_URL).toString();

		SolrServer server = new HttpSolrServer(solrUrl);
		try {
			server.add(doc);
			server.commit();
		} catch (SolrServerException e) {
			throw new SystemException(SystemErrorMessage.SOLR_OPERARION_FAILURE, e);
		}catch (IOException e){
			throw new SystemException(SystemErrorMessage.SOLR_OPERARION_FAILURE, e);
		}
		
	}

	@Override
	public void updateDocument(CustomerVo customerVo) throws SystemException {
		SolrInputDocument doc = new SolrInputDocument();
		doc.addField("custId", customerVo.getCustId());
		doc.addField("namEng", customerVo.getNamEng());
		doc.addField("namZht", customerVo.getNamZht());
		doc.addField("namZhs", customerVo.getNamZhs());
		doc.addField("birthday", customerVo.getDobMonth() + "-" + customerVo.getDobDay());
		if(customerVo.getCustomerEmailList() != null){
			for(CustomerEmailVo emailVo : customerVo.getCustomerEmailList()){
				if(ConstantUtil.COMMUNICATE_TYPE_CONTACT.equals(emailVo.getTypeCde())){
					doc.addField("email", emailVo.getEmail());
				}
			}
		}
		
		if(customerVo.getCustomerTelephoneList() != null){
			for(CustomerTelephoneVo telephoneVo : customerVo.getCustomerTelephoneList()){
				if(ConstantUtil.COMMUNICATE_TYPE_CONTACT.equals(telephoneVo.getTypeCde())){
					doc.addField("telNbr", telephoneVo.getTelNbr());
				}
			}
		}		

		String solrUrl = ResourceUtil.getSolrConfig().get(ConstantUtil.SOLR_SERVER_URL).toString();

		SolrServer server = new HttpSolrServer(solrUrl);
		try {
			server.deleteById(customerVo.getCustId());
			server.add(doc);
			server.commit();
		} catch (SolrServerException e) {
			throw new SystemException(SystemErrorMessage.SOLR_OPERARION_FAILURE, e);
		}catch (IOException e){
			throw new SystemException(SystemErrorMessage.SOLR_OPERARION_FAILURE, e);
		}
		
	}

}
